<?php
/**
 * @authors alex (20779512@qq.com)
 * @date    2014-04-15 08:58:26
 * @version 1.0
 */

class CategoryAction extends AdminAction {
    
    Public $arr = array();

	public function _initialize(){    	

		parent::_initialize();

		$where['user'] = array('like','%'.$this->adminName.'%');
		$list = M('Category')->field('id')->where($where)->select();
		foreach ($list as $key => $value) {
			$this->arr[] = $value['id'];
			$this->getClassID($value['id'],$arr);
		}

	}

    public function getClassID($fid,$arr){
		$map['fid']=$fid;
		$list = M('Category')->field('id')->where($map)->select();
		foreach ($list as $key => $value) {			
			$this->arr[] = $value['id'];
			$this->getClassID($list['id'],$arr);
		}
	}

	#文章分类列表
	function index() {

		$artClass = M('Category');
		$list = $artClass->field("id,name,user,fid,path,sort")->order('path,id asc')->select();
		foreach ($list as $key => $value) {
			$count = count(explode('-', $value['path'])) - 2;
			if ($value['fid'] > 0) {
				$list[$key]['style'] = 'style="padding-left:' . (($count * 10) + 10) . 'px;"';
			}
		}

		$this->assign('list', $list);
		$this->display();
	}
	
	/*
	 *分类部分的增删改操作
	 */
 	function add(){

		$fid=I('get.fid',0);
		$path=I('get.path','0-');

 		$artClass = M('Category');

		$list = $artClass->field("id,name,fid,path")->order('path')->select();

		foreach ($list as $key => $value) {
			$count = count(explode('-', $value['path'])) - 3;
			$list[$key]['count'] = $count;

		}

		$this->assign('list', $list);
		$this->assign('fid', $fid);
		$this->assign('path', $path);
		$this->display();
 	}


	function edit(){

		$id = I('get.id');
		if(isset($id)){
			$artClass = M('Category');
			$clist = $artClass->field("id,name,fid,path")->order('path')->select();
			foreach ($clist as $key => $value) {
			$count = count(explode('-', $value['path'])) - 3;
			$clist[$key]['count'] = $count;
			}
			$cate = M('Category');
			$list=$cate->where('id='.$id)->find();
			if($list){
				$this->assign('list',$list);
				$this->assign('clist', $clist);
				$this->display();
			}else{
				$this->error('没有该分类');
			}
		}else{
			$this->error('参数错误');
		}
	}


	function save(){

		$cate = D('Category');

		if($date = $cate->create()){

			if ($list=$cate->add($date)) {

				$date['path']=$date['path'].$list.'-';

				$cate->where('id='.$list)->save($date);

				$state = 'SUCCESS';
                $message = '分类添加成功';
                $url = U('Category/index');

			} else {
				$state = '分类添加失败';
			}

		}else{
			$state = $cate->getError();
		}
		echo $this->echo_json_str($state,$message,$url);
	}


	function update(){

		$cate = D('Category');

		if ($date = $cate->create()) {

			if($date['id']==$date['fid']){
				$state = '不能以自身为上级栏目';
				echo $this->echo_json_str($state,$message,$url);
				die;
			}

			$thisFID = $_POST['thisFID'];

			if($thisFID==0 && $date['fid']>0){
				$state = '根栏目不能移动';
				echo $this->echo_json_str($state,$message,$url);
				die;
			}

			if($date['fid']>0){

				$tClass = M('Category');

				$fdate = $tClass->field('path')->where('id='.$date['fid'])->find();

				if(strstr($fdate['path'],$date['path'])){

					$state = '不能移动到自身下级栏目';
					echo $this->echo_json_str($state,$message,$url);
					die;
				}
				$date['path']=$fdate['path'].$date['id'].'-';
			}else{
				$date['path']=I('post.path');
			}

			$oldPath = I('post.path');

			if ($cate->save($date)) {

				$cate->query("UPDATE ty_category SET path = replace(path, '".$oldPath."','".$date['path']."')");
				$state = 'SUCCESS';
				$message = '分类编辑成功';
				$url = U('Category/index');

			} else {
				$state = '分类编辑失败';
			}

		} else {
			$state = $cate->getError();
		}

		echo $this->echo_json_str($state,$message,$url);

	}


	function del(){

		$id = I('get.id');
		if(!isset($id)){
		$this->error("您没有选择任何分类！");
		}

		$cate = M('Category');

		$list = $cate->where('fid='.$id)->find();

		if($list){
			echo '2';die;
		}

		$list = $cate->where('id='.$id)->delete();

		$article=M('Article');

		$article->where('cid='.$id)->delete();

		if($list){
			echo '1';
		}else{
			echo '0';
		}

	}
}